#!/usr/bin/perl
use DBI qw(:sql_types);
require "prefs.pl";

print "Content-Type: text/html\n\n";
my $db_filename="/home/tvl/dev/dns/dns.db";

my $dbh = DBI->connect("dbi:SQLite:$db_filename");

sub create_conf_file
{
	my $ns_type= $_[0];	
	if ($ns_type eq "master") { open( ConfFile, $pri_file) || die};
	if ($ns_type eq "slave") { open( ConfFile, $sec_file) || die};
	$rc  = $dbh->begin_work;
	$sth = $dbh->prepare("SELECT name FROM domains");
	$sth->execute();
	while ( @row = $sth->fetchrow_array ) {
		if ($ns_type eq "master") {
			&create_zone_file($row[0]);
			print ConfFile "// $row[0]\n",
				"zone \"$row[0]\" {\n",
				"\ttype $_[0];\n",
				"\tfile \"$row[0].db\";\n",
				"};\n\n";
		} else {
			print ConfFile "// $row[0]\n",
				"zone \"$row[0]\" {\n",
				"\ttype $_[0];\n",
				"\tfile \"$row[0].bk\";\n",
				"masters { $ns1_ip; };\n",
				"};\n\n";
		}
			
	}
	$rc  = $dbh->commit;
	close ( ConfFile );
}	

sub create_zone_file
{
	my $zone_name= $_[0];
	my $t = gmtime(time);
	open( ZoneFile, ">$pri_zones_dir$zone_name.db") || die;
	print ZoneFile ";\n", ";BIND data file for $zone_name zone\n", ";$t\n",";\n",
		"\$TTL    604800\n",
		"@       IN      SOA     localhost. root.localhost. (\n",
		"                              1         ; Serial\n",
		"                         604800         ; Refresh\n",
		"                          86400         ; Retry\n",
		"                        2419200         ; Expire\n",
		"                         604800 )       ; Negative Cache TTL\n",
		";\n",
		"@       IN      NS      $ns1_ip\n",
		"@       IN      NS      $ns2_ip\n";
	close ( ZoneFile );	
}	

&create_conf_file("master");
&create_conf_file("slave");


$rc  = $dbh->disconnect;